//
//  ChangeNavSupport.swift
//  Aiwei
//
//  Created by zhu xietong on 2017/11/16.
//  Copyright © 2017年 zhu xietong. All rights reserved.
//

import Foundation
import Eelay

public enum NavTransparent{
    case yes
    case no
    case `init`
    case opacity(CGFloat)
}

open class NavChangeASCollectionContainer: ASCollectionContainer,UIGestureRecognizerDelegate{
    override open var initTopStyle: EeTopStyle{
        var style:EeTopStyle = .default
        style.navBarBgAlpha = 0
        style.navBarShadow = .none
        //        style.navBarBgStyle = .color(.blue)
        style.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.clear]
        style.navTintColor = .white
        style.statusBarStyle = .default
        return style
    }
    
    public let backBt = UIButton(frame: [26,26])
    public weak var __gesture_delegate:UIGestureRecognizerDelegate? = nil
    
    public var navChangeOffsetY:CGFloat = 200
    public var tranpresentTopStyle = EeTopStyle.navTraspresent()
    public var notranpresentTopStyle = EeTopStyle.default
    public var defaultTopStyle = EeTopStyle.default

    
    
    open func buildBackBtChangeNavSupport() {
        backBt.setImage(UIImage(named: "nav_back_shadow"), for: .normal)
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backBt)
        backBt.addTarget(self, action: #selector(bkButtonPress), for: .touchUpInside)
    }
    
    @objc open func bkButtonPress() {
        dismissAction()
        //        _ = self.navigationController?.popViewController(animated: true)
    }

    
    open override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.assignGesgureDelegate()
    }
    
    open override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.releaseGesgureDelegate()
    }
    
    
    open func assignGesgureDelegate() {
        if let nav = self.navigationController
        {
            if nav.viewControllers.count > 1
            {
                __gesture_delegate = nav.interactivePopGestureRecognizer?.delegate
                nav.interactivePopGestureRecognizer?.delegate = self
            }
        }
    }
    
    open func releaseGesgureDelegate() {
        if let _delegate = self.navigationController?.interactivePopGestureRecognizer?.delegate
        {
            if _delegate === self
            {
                self.navigationController?.interactivePopGestureRecognizer?.delegate = __gesture_delegate
            }
        }
    }
    
    open func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.observerScroll(scrollView: scrollView)
        
    }
    
    open func observerScroll(scrollView:UIScrollView)
    {
        if self.appearStatus != .didAppear
        {
            return
        }
        let contentOffsetY = scrollView.contentOffset.y
        let showNavBarOffsetY = navChangeOffsetY - topLayoutGuide.length
        //navigationBar alpha
        if contentOffsetY > showNavBarOffsetY  {
            var navAlpha = (contentOffsetY - (showNavBarOffsetY)) / 40.0
            if navAlpha > 1 {
                navAlpha = 1
            }
            if navAlpha < 0
            {
                navAlpha = 0
            }
            //            self.navigationController?.navigationBar.centerBack.alpha = navAlpha
            top_style.navBarBgAlpha = navAlpha
            if navAlpha > 0.5 {
                top_style.navTintColor = notranpresentTopStyle.navTintColor
                top_style.statusBarStyle = notranpresentTopStyle.statusBarStyle
                top_style.titleTextAttributes = notranpresentTopStyle.titleTextAttributes
                self.updateNavTransparent(transparent: .no)
            }else{
                top_style.navTintColor = tranpresentTopStyle.navTintColor
                top_style.statusBarStyle = tranpresentTopStyle.statusBarStyle
                top_style.titleTextAttributes = tranpresentTopStyle.titleTextAttributes
                self.updateNavTransparent(transparent: .yes)
                
            }
        }else{
            
            top_style.navBarBgAlpha = 0
            top_style.navTintColor = tranpresentTopStyle.navTintColor
            top_style.statusBarStyle = tranpresentTopStyle.statusBarStyle
            top_style.titleTextAttributes = tranpresentTopStyle.titleTextAttributes
            self.updateNavTransparent(transparent: .yes)
            
        }
    }
    
    
    open func updateNavTransparent(transparent:NavTransparent) {
        switch transparent {
        case .no:
            let back_icon = UIImage(named: "back-indicator")?.rt_tintedImage(with: self.notranpresentTopStyle.navTintColor)
            top_style.navBarShadow = .color(.clear)
            top_style.navBarShadow = notranpresentTopStyle.navBarShadow
            backBt.setImage(back_icon, for: .normal)
        case .yes:
            backBt.setImage(UIImage(named: "back-indicator-shadow"), for: .normal)
            top_style.navBarShadow = .color(.clear)
        default:
            break
        }
    }
    
}


open class NavChangeASTableContainer: ASTableContainer,UIGestureRecognizerDelegate{
    override open var initTopStyle: EeTopStyle{
        var style:EeTopStyle = .default
        style.navBarBgAlpha = 0
        style.navBarShadow = .none

        //        style.navBarBgStyle = .color(.blue)
        style.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.clear]
        style.navTintColor = .white
        style.statusBarStyle = .default
        return style
    }
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        updateNavTransparent(transparent: .yes)
    }
    
    public let backBt = UIButton(frame: [26,26])
    public weak var __gesture_delegate:UIGestureRecognizerDelegate? = nil
    
    public var navChangeOffsetY:CGFloat = 200
    public var tranpresentTopStyle = EeTopStyle.navTraspresent()
    public var notranpresentTopStyle = EeTopStyle.default
    
    
    open func buildBackBtChangeNavSupport() {
        backBt.setImage(UIImage(named: "nav_back_shadow"), for: .normal)
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backBt)
        backBt.addTarget(self, action: #selector(bkButtonPress), for: .touchUpInside)
        
    }
    
    @objc open func bkButtonPress() {
        dismissAction()
        //        _ = self.navigationController?.popViewController(animated: true)
    }
    
    
    open override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.assignGesgureDelegate()
    }
    
    open override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.releaseGesgureDelegate()
    }
    
    
    open func assignGesgureDelegate() {
        if let nav = self.navigationController
        {
            if nav.viewControllers.count > 1
            {
                __gesture_delegate = nav.interactivePopGestureRecognizer?.delegate
                nav.interactivePopGestureRecognizer?.delegate = self
            }
        }
    }
    
    open func releaseGesgureDelegate() {
        if let _delegate = self.navigationController?.interactivePopGestureRecognizer?.delegate
        {
            if _delegate === self
            {
                self.navigationController?.interactivePopGestureRecognizer?.delegate = __gesture_delegate
            }
        }
    }
    
    open func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.observerScroll(scrollView: scrollView)
    }
    
    
    open func observerScroll(scrollView:UIScrollView)
    {
        if self.appearStatus != .didAppear
        {
            return
        }
        let contentOffsetY = scrollView.contentOffset.y
        let showNavBarOffsetY = navChangeOffsetY - topLayoutGuide.length
        //navigationBar alpha
        if contentOffsetY > showNavBarOffsetY  {
            var navAlpha = (contentOffsetY - (showNavBarOffsetY)) / 40.0
            if navAlpha > 1 {
                navAlpha = 1
            }
            if navAlpha < 0
            {
                navAlpha = 0
            }
            //            self.navigationController?.navigationBar.centerBack.alpha = navAlpha
            top_style.navBarBgAlpha = navAlpha
            if navAlpha > 0.5 {
                top_style.navTintColor = notranpresentTopStyle.navTintColor
                top_style.statusBarStyle = notranpresentTopStyle.statusBarStyle
                top_style.titleTextAttributes = notranpresentTopStyle.titleTextAttributes
                self.updateNavTransparent(transparent: .no)
            }else{
                top_style.navTintColor = tranpresentTopStyle.navTintColor
                top_style.statusBarStyle = tranpresentTopStyle.statusBarStyle
                top_style.titleTextAttributes = tranpresentTopStyle.titleTextAttributes
                self.updateNavTransparent(transparent: .yes)
                
            }
        }else{
            top_style.navBarBgAlpha = 0
            top_style.navTintColor = tranpresentTopStyle.navTintColor
            top_style.statusBarStyle = tranpresentTopStyle.statusBarStyle
            top_style.titleTextAttributes = tranpresentTopStyle.titleTextAttributes
            self.updateNavTransparent(transparent: .yes)
        }
    }
    
    
     open func updateNavTransparent(transparent:NavTransparent) {
         switch transparent {
         case .no:
             let back_icon = UIImage(named: "back-indicator")?.rt_tintedImage(with: self.notranpresentTopStyle.navTintColor)
             top_style.navBarShadow = .color(.clear)
             top_style.navBarShadow = notranpresentTopStyle.navBarShadow
             backBt.setImage(back_icon, for: .normal)
         case .yes:
             backBt.setImage(UIImage(named: "back-indicator-shadow"), for: .normal)
             top_style.navBarShadow = .color(.clear)
         default:
             break
         }
     }
    
}


open class NavChangeASListController: ASListController,UIGestureRecognizerDelegate{
    override open var initTopStyle: EeTopStyle{
        var style:EeTopStyle = .default
        style.navBarBgAlpha = 0
        style.navBarShadow = .none

        //        style.navBarBgStyle = .color(.blue)
        style.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.clear]
        style.navTintColor = .white
        style.statusBarStyle = .default
        return style
    }
    
    public let backBt = UIButton(frame: [26,26])
    public weak var __gesture_delegate:UIGestureRecognizerDelegate? = nil
    
    public var navChangeOffsetY:CGFloat = 200
    public var tranpresentTopStyle = EeTopStyle.navTraspresent()
    public var notranpresentTopStyle = EeTopStyle.default
    
    
    open func buildBackBtChangeNavSupport() {
        backBt.setImage(UIImage(named: "nav_back_shadow"), for: .normal)
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backBt)
        backBt.addTarget(self, action: #selector(bkButtonPress), for: .touchUpInside)
    }
    open func buildRightChangeNavSupport(){
        
    }
    
    
    @objc open func bkButtonPress() {
        dismissAction()
        //        _ = self.navigationController?.popViewController(animated: true)
    }
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        updateNavTransparent(transparent: .yes)
    }
    
    open override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.assignGesgureDelegate()
    }
    
    open override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.releaseGesgureDelegate()
    }
    
    
    open func assignGesgureDelegate() {
        if let nav = self.navigationController
        {
            if nav.viewControllers.count > 1
            {
                __gesture_delegate = nav.interactivePopGestureRecognizer?.delegate
                nav.interactivePopGestureRecognizer?.delegate = self
            }
        }
    }
    
    open func releaseGesgureDelegate() {
        if let _delegate = self.navigationController?.interactivePopGestureRecognizer?.delegate
        {
            if _delegate === self
            {
                self.navigationController?.interactivePopGestureRecognizer?.delegate = __gesture_delegate
            }
        }
    }
    
    open func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.observerScroll(scrollView: scrollView)
        
    }
    
    open func observerScroll(scrollView:UIScrollView)
    {
        if self.appearStatus != .didAppear
        {
            return
        }
        let contentOffsetY = scrollView.contentOffset.y
        let showNavBarOffsetY = navChangeOffsetY - topLayoutGuide.length
        //navigationBar alpha
        if contentOffsetY > showNavBarOffsetY  {
            var navAlpha = (contentOffsetY - (showNavBarOffsetY)) / 40.0
            if navAlpha > 1 {
                navAlpha = 1
            }
            if navAlpha < 0
            {
                navAlpha = 0
            }
            //            self.navigationController?.navigationBar.centerBack.alpha = navAlpha
            top_style.navBarBgAlpha = navAlpha
            if navAlpha > 0.5 {
                top_style.navTintColor = notranpresentTopStyle.navTintColor
                top_style.statusBarStyle = notranpresentTopStyle.statusBarStyle
                top_style.titleTextAttributes = notranpresentTopStyle.titleTextAttributes
                self.updateNavTransparent(transparent: .no)
            }else{
                top_style.navTintColor = tranpresentTopStyle.navTintColor
                top_style.statusBarStyle = tranpresentTopStyle.statusBarStyle
                top_style.titleTextAttributes = tranpresentTopStyle.titleTextAttributes
                self.updateNavTransparent(transparent: .yes)
                
            }
        }else{
            
            top_style.navBarBgAlpha = 0
            top_style.navTintColor = tranpresentTopStyle.navTintColor
            top_style.statusBarStyle = tranpresentTopStyle.statusBarStyle
            top_style.titleTextAttributes = tranpresentTopStyle.titleTextAttributes
            self.updateNavTransparent(transparent: .yes)
            
        }
    }
    
    
     open func updateNavTransparent(transparent:NavTransparent) {
         switch transparent {
         case .no:
             let back_icon = UIImage(named: "back-indicator")?.rt_tintedImage(with: self.notranpresentTopStyle.navTintColor)
             top_style.navBarShadow = .color(.clear)
             top_style.navBarShadow = notranpresentTopStyle.navBarShadow
             backBt.setImage(back_icon, for: .normal)
         case .yes:
             backBt.setImage(UIImage(named: "back-indicator-shadow"), for: .normal)
             top_style.navBarShadow = .color(.clear)
         default:
             break
         }
     }
    
}


open class NavChangeTableContainer: TableContainer,UIGestureRecognizerDelegate{
    
    override open var initTopStyle: EeTopStyle{
        var style:EeTopStyle = .default
        style.navBarBgAlpha = 0
        style.navBarShadow = .none

        //        style.navBarBgStyle = .color(.blue)
        style.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.clear]
        style.navTintColor = .white
        style.statusBarStyle = .default
        return style
    }
    
    public let backBt = UIButton(frame: [26,26])
    public weak var __gesture_delegate:UIGestureRecognizerDelegate? = nil
    
    public var navChangeOffsetY:CGFloat = 200
    public var tranpresentTopStyle = EeTopStyle.navTraspresent()
    public var notranpresentTopStyle = EeTopStyle.default
    
    
    open func buildBackBtChangeNavSupport() {
        backBt.setImage(UIImage(named: "nav_back_shadow"), for: .normal)
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backBt)
        backBt.addTarget(self, action: #selector(bkButtonPress), for: .touchUpInside)
        
    }
    
    @objc open func bkButtonPress() {
        dismissAction()
        
        //        _ = self.navigationController?.popViewController(animated: true)
    }
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        updateNavTransparent(transparent: .yes)
    }
    
    open override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.assignGesgureDelegate()
    }
    
    open override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.releaseGesgureDelegate()
    }
    
    
    open func assignGesgureDelegate() {
        if let nav = self.navigationController
        {
            if nav.viewControllers.count > 1
            {
                __gesture_delegate = nav.interactivePopGestureRecognizer?.delegate
                nav.interactivePopGestureRecognizer?.delegate = self
            }
        }
    }
    
    open func releaseGesgureDelegate() {
        if let _delegate = self.navigationController?.interactivePopGestureRecognizer?.delegate
        {
            if _delegate === self
            {
                self.navigationController?.interactivePopGestureRecognizer?.delegate = __gesture_delegate
            }
        }
    }
    
    open func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.observerScroll(scrollView: scrollView)
        
    }
    
    open func observerScroll(scrollView:UIScrollView)
    {
        if self.appearStatus != .didAppear
        {
            return
        }
        let contentOffsetY = scrollView.contentOffset.y
        let showNavBarOffsetY = navChangeOffsetY - topLayoutGuide.length
        //navigationBar alpha
        if contentOffsetY > showNavBarOffsetY  {
            var navAlpha = (contentOffsetY - (showNavBarOffsetY)) / 40.0
            if navAlpha > 1 {
                navAlpha = 1
            }
            if navAlpha < 0
            {
                navAlpha = 0
            }
            //            self.eenavbar.alpha = navAlpha
            top_style.navBarBgAlpha = navAlpha
            if navAlpha > 0.5 {
                top_style.navTintColor = notranpresentTopStyle.navTintColor
                top_style.statusBarStyle = notranpresentTopStyle.statusBarStyle
                top_style.titleTextAttributes = notranpresentTopStyle.titleTextAttributes
                self.updateNavTransparent(transparent: .no)
            }else{
                top_style.navTintColor = tranpresentTopStyle.navTintColor
                top_style.statusBarStyle = tranpresentTopStyle.statusBarStyle
                top_style.titleTextAttributes = tranpresentTopStyle.titleTextAttributes
                self.updateNavTransparent(transparent: .yes)
                
            }
        }else{
            
            
            top_style.navBarBgAlpha = 0
            top_style.navTintColor = tranpresentTopStyle.navTintColor
            top_style.statusBarStyle = tranpresentTopStyle.statusBarStyle
            top_style.titleTextAttributes = tranpresentTopStyle.titleTextAttributes
            self.updateNavTransparent(transparent: .yes)
            
        }
    }
    
    
     open func updateNavTransparent(transparent:NavTransparent) {
           switch transparent {
           case .no:
               let back_icon = UIImage(named: "back-indicator")?.rt_tintedImage(with: self.notranpresentTopStyle.navTintColor)
               top_style.navBarShadow = .color(.clear)
               top_style.navBarShadow = notranpresentTopStyle.navBarShadow
               backBt.setImage(back_icon, for: .normal)
           case .yes:
               backBt.setImage(UIImage(named: "back-indicator-shadow"), for: .normal)
               top_style.navBarShadow = .color(.clear)
           default:
               break
           }
       }
    
    
}

extension EeTopStyle{
    public static func navTraspresent()->EeTopStyle
    {
        var style = EeTopStyle()
        style.navTintColor = .white
        style.statusBarStyle = .lightContent
        style.navBarBgStyle = .color(.white)
        style.navBarBgAlpha = 0
        style.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.clear]
        return style
    }
    
}


open class NavChangeListController: ListController,UIGestureRecognizerDelegate{
    override open var initTopStyle: EeTopStyle{
        var style:EeTopStyle = .default
        style.navBarBgAlpha = 0
        //        style.navBarBgStyle = .color(.blue)
        style.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.clear]
        style.navTintColor = .white
        style.statusBarStyle = .default
        return style
    }
    
    public let backBt = UIButton(frame: [26,26])
    public weak var __gesture_delegate:UIGestureRecognizerDelegate? = nil
    
    public var navChangeOffsetY:CGFloat = 200
    public var tranpresentTopStyle = EeTopStyle.navTraspresent()
    public var notranpresentTopStyle = EeTopStyle.default
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        updateNavTransparent(transparent: .yes)
    }
    
    
    public func buildBackBtChangeNavSupport() {
        backBt.setImage(UIImage(named: "nav_back_shadow"), for: .normal)
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backBt)
        backBt.addTarget(self, action: #selector(bkButtonPress), for: .touchUpInside)
        
    }
    
    @objc open func bkButtonPress() {
        dismissAction()
        //        _ = self.navigationController?.popViewController(animated: true)
    }
    
    
    open override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.assignGesgureDelegate()
    }
    
    open override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.releaseGesgureDelegate()
    }
    
    
    open func assignGesgureDelegate() {
        if let nav = self.navigationController
        {
            if nav.viewControllers.count > 1
            {
                __gesture_delegate = nav.interactivePopGestureRecognizer?.delegate
                nav.interactivePopGestureRecognizer?.delegate = self
            }
        }
    }
    
    open func releaseGesgureDelegate() {
        if let _delegate = self.navigationController?.interactivePopGestureRecognizer?.delegate
        {
            if _delegate === self
            {
                self.navigationController?.interactivePopGestureRecognizer?.delegate = __gesture_delegate
            }
        }
    }
    
    open func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.observerScroll(scrollView: scrollView)
        
    }
    
    open func observerScroll(scrollView:UIScrollView)
    {
        if self.appearStatus != .didAppear
        {
            return
        }
        let contentOffsetY = scrollView.contentOffset.y
        let showNavBarOffsetY = navChangeOffsetY - topLayoutGuide.length
        //navigationBar alpha
        if contentOffsetY > showNavBarOffsetY  {
            var navAlpha = (contentOffsetY - (showNavBarOffsetY)) / 40.0

            if navAlpha > 1 {
                navAlpha = 1
            }
            if navAlpha < 0
            {
                navAlpha = 0
            }
            //            self.navigationController?.navigationBar.centerBack.alpha = navAlpha
            top_style.navBarBgAlpha = navAlpha
            if navAlpha > 0.5 {
                top_style.navTintColor = notranpresentTopStyle.navTintColor
                top_style.statusBarStyle = notranpresentTopStyle.statusBarStyle
                top_style.titleTextAttributes = notranpresentTopStyle.titleTextAttributes

                self.updateNavTransparent(transparent: .no)
            }else{
                top_style.navTintColor = tranpresentTopStyle.navTintColor
                top_style.statusBarStyle = tranpresentTopStyle.statusBarStyle
                top_style.titleTextAttributes = tranpresentTopStyle.titleTextAttributes
                self.updateNavTransparent(transparent: .yes)
                
            }
        }else{
            
            
            top_style.navBarBgAlpha = 0
            top_style.navTintColor = tranpresentTopStyle.navTintColor
            top_style.statusBarStyle = tranpresentTopStyle.statusBarStyle
            top_style.titleTextAttributes = tranpresentTopStyle.titleTextAttributes
            self.updateNavTransparent(transparent: .yes)
            
        }
    }
    
    
    open func updateNavTransparent(transparent:NavTransparent) {
        switch transparent {
        case .no:
            let back_icon = UIImage(named: "back-indicator")?.rt_tintedImage(with: self.notranpresentTopStyle.navTintColor)
            backBt.setImage(back_icon, for: .normal)
        case .yes:
            backBt.setImage(UIImage(named: "back-indicator-shadow"), for: .normal)
            
        default:
            break
        }
        
    }
    
}

